<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册主页面</title>
    <link rel="stylesheet" href="./layui/css/layui.css">
    <script type="text/javascript" src="jquery-1.9.1.js"></script>
    <style type="text/css">
        .ddiv{
            width: 1000px;
            height: 500px;
            margin-top: 200px;
            margin-left: 400px;
        }
        .layui-input{
            width: 245px;
        }
    </style>
</head>
<body>

        <div class="ddiv">
            <form class="layui-form" action="/sign.do" method="post">
                <div class="layui-form-item">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-block">
                        <input type="text" id="account" name="username" required lay-verify="required" placeholder="请输入用户名" autocomplete="off" class="layui-input">&nbsp;<span class="accou">√</span>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">密码框</label>
                    <div class="layui-input-inline">
                        <input type="password" id="password" name="password" required lay-verify="number" placeholder="请输入密码" autocomplete="off" class="layui-input">&nbsp;<span class="pass">√</span>
                    </div>
                </div>
                <div>
                    <label class="layui-form-label">邮箱</label>
                    <div class="layui-input-inline">
                        <input type="email" id="email" name="email" required lay-verify="email" placeholder="请输入邮箱" autocomplete="off" class="layui-input">&nbsp;<span class="mail">√</span>
                    </div>
                </div>
                <br>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="formDemo" name="submit">立即提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </div>


        <script type="text/javascript">
            var accountRegexp = /^[a-zA-z][A-Za-z1-9_-]+$/;
            var passwordRegexp = /^[a-zA-z][A-Za-z1-9_-]+$/;
            var mailRegexp = /^[1-9]\d{5,10}@(qq|163|139)\.com$/;
            $(function () {
                $("#account").blur(function () {
                    if ($(this).val().search(accountRegexp) != -1){
                        $(".accou").css({"color": "#3cb371", "display": "inline-block"}).html("√");
                        //用ajax发一个请求给servlet
                        $.ajax({
                            "url":"/check.do",
                            "data":{"username":$("#account").val()
                            },
                            "dataType":"json",
                            "type":"POST"
                        }).done(function (data) {
                            if(data.username != null && data.username.length >0){
                                if($("#account").val().search(accountRegexp) != -1){
                                    $(".main").html("注册成功......");
                                    $(".layui-btn").removeAttr("disabled");
                                }
                            }else{
                                $(".layui-btn").attr("disabled","disabled");
                            }
                        });
                    } else {
                        $(".accou").css({"color": "#f00", "display": "inline-block"}).html("×");
                    }
                });
                $("#password").blur(function () {
                    if ($(this).val().search(passwordRegexp) != -1){
                        $(".pass").css({"color": "#3cb371", "display": "inline-block"}).html("√");
                    } else {
                        $(".pass").css({"color": "#f00", "display": "inline-block"}).html("×");
                        $(".layui-btn").attr("disabled","disabled");
                    }
                });
                $("#email").blur(function () {

                    var ret = mailRegexp.test($(this).val());
                    if ($(this).val().search(mailRegexp) != -1) {
                        var mailType = $(this).val().match(mailRegexp)[1];
                        $(".mail").css({"color": "#3cb371", "display": "inline-block"}).html(mailType + "邮箱格式正确");
                    } else {
                        $(".mail").css({"color": "#f00", "display": "inline-block"}).html("邮箱错误!");
                        $(".layui-btn").attr("disabled","disabled");
                    }
                });

            });

        </script>
</body>
</html>